package com.browsesoft.sign;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import org.hibernate.HibernateException;
import org.hibernate.Session;

import com.browsesoft.hibernate.HibernateUtil;

/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description: 签名Web服务，保存和管理客户端签名
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2004
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 * 
 * @author not attributable
 * @version 1.0
 */
public class SignService extends UnicastRemoteObject implements SignServiceRMI {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public SignService() throws RemoteException {
		super();
	}

	/**
	 * 保存签名
	 * 
	 * @return 签名标识
	 */
	public String setSign(String id, String value) throws RemoteException {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Sign sign = null;
		if (id == null || id.equals("")) {
			sign = new Sign(id, value);
			session.save(sign);
		} else {
			sign = (Sign) session.load(Sign.class, id);
			sign.setValue(value);
		}
		session.getTransaction().commit();
		return sign.getId();
	}

	/**
	 * 得到签名
	 * 
	 */
	public String getSign(String id) throws RemoteException {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.beginTransaction();
			Sign sign = (Sign) session.load(Sign.class, id);
			return sign.getValue();
		} catch (HibernateException ex) {
			return "";
		}
	}
}
